package ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class RuleFrame extends JFrame {
	private JButton startBt;
	private JButton stopBt;
	private JTextArea crTextArea;
	private Thread readThread;
	private boolean readFlag = true;
	private JButton backBt;

	public RuleFrame() {
		this.setTitle("GameRule(游戏规则)");
		this.setSize(400, 500);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setResizable(false);
		Toolkit tool = Toolkit.getDefaultToolkit();
		Dimension d = tool.getScreenSize();
		this.setLocation((d.width - getWidth()) / 2, (d.height - getHeight()) / 2);

		uiInit();
		addActionListener();
	}

	private void uiInit() {
		Container container = this.getContentPane();
		container.setLayout(new BorderLayout());

		crTextArea = new JTextArea();
		crTextArea.setEditable(false);

		JScrollPane scrollPane = new JScrollPane(crTextArea);
		startBt = new JButton("开始读取");
		stopBt = new JButton("停止读取");
		backBt = new JButton("关闭界面");
		stopBt.setEnabled(false);

		JPanel flowPanel = new JPanel(new FlowLayout(1, 30, 5));

		flowPanel.add(startBt);
		flowPanel.add(stopBt);
		flowPanel.add(backBt);

		container.add(scrollPane, BorderLayout.CENTER);
		container.add(flowPanel, BorderLayout.SOUTH);
	}

	private void printDeveloperListInformation() {
		if (readThread == null) {
			try {
				readThread = new Thread(new Runnable() {
					BufferedReader bufferedReader = new BufferedReader(new FileReader("res/txt/gamerule.txt"));
					int len = 0;
					char[] bs = new char[1];

					@Override
					public void run() {
						while (readFlag) {
							if (readThread.isAlive()) {
								if (!startBt.isEnabled()) {
									try {
										if ((len = bufferedReader.read(bs)) > 0) {
											crTextArea.append(new String(bs, 0, len));
											crTextArea.selectAll();
											crTextArea.setCaretPosition(crTextArea.getSelectedText().length());
										} else {
											stopBt.setEnabled(false);
											bufferedReader.close();
											return;
										}
										Thread.sleep(50);
									} catch (IOException | InterruptedException e) {
										e.printStackTrace();
									}
								}
							}
						}
					}
				});
				readThread.start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void addActionListener() {
		startBt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				startBt.setEnabled(false);
				stopBt.setEnabled(true);
				printDeveloperListInformation();
			}

		});

		stopBt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				startBt.setEnabled(true);
				stopBt.setEnabled(false);
			}
		});
		backBt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
	}
}
